/*
 * Created on 28/mag/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc.nip;

import it.unibo.deis.lia.networkInterface.*;

import java.util.*;

public class NetworkInterfaceProvider {

	private Vector<GenericNetworkInterface> availableInterfaces = new Vector<GenericNetworkInterface>();
	private static NetworkInterfaceProvider networkInterfaceProvider = null;
	private static String myUniversalName = null;

	// TODO: portare la roba in GenericNetworkInterface.getInterfaces qui,
	// creando direttamente gli oggetti opportuni?
	private NetworkInterfaceProvider() /* throws Exception */{
		/**/
		try {
			Bluetooth bluetooth = new Bluetooth(0);

			// NOTE: the line below is really important:
			// without it the bluetooth.getRssiList() method will be blocking
			bluetooth.startMonitoringRemoteDevices();

			availableInterfaces.addElement(bluetooth);
		} catch (Exception e) {
			// No Bluetooth
			e.printStackTrace();
			System.out.println("NetworkInterfaceProvider: no Bluetooth");
		}/**/
		try {
			Bluetooth bluetooth = new Bluetooth(1);

			// NOTE: the line below is really important:
			// without it the bluetooth.getRssiList() method will be blocking
			bluetooth.startMonitoringRemoteDevices();

			availableInterfaces.addElement(bluetooth);
		} catch (Exception e) {
			// No Bluetooth
			e.printStackTrace();
			System.out.println("NetworkInterfaceProvider: no Bluetooth");
		}/**/

		/**/try {
			IEEE80211 ieee80211 = new IEEE80211(0);
			// ieee80211.setConnectorRole("pippo22", "192.168.22.1", null);
			availableInterfaces.addElement(ieee80211);
		} catch (Exception e) {
			// No IEEE 802.11
			e.printStackTrace();
			System.out.println("NetworkInterfaceProvider: no IEEE80211");
		}/**/
		try {
			IEEE80211 ieee80211 = new IEEE80211(1);
			// ieee80211.setConnectorRole("pippo22", "192.168.22.1", null);
			availableInterfaces.addElement(ieee80211);
		} catch (Exception e) {
			// No IEEE 802.11
			e.printStackTrace();
			System.out.println("NetworkInterfaceProvider: no IEEE80211");
		}/**/

		/**/try {
			Ethernet ethernet = new Ethernet();
			availableInterfaces.addElement(ethernet);
		} catch (Exception e) {
			// No Ethernet
			e.printStackTrace();
			System.out.println("NetworkInterfaceProvider: no Ethernet");
		}/**/
	}

	public synchronized static NetworkInterfaceProvider getInstance()
			throws Exception {
		if (NetworkInterfaceProvider.networkInterfaceProvider == null)
			NetworkInterfaceProvider.networkInterfaceProvider = new NetworkInterfaceProvider();
		if (NetworkInterfaceProvider.myUniversalName == null)
			NetworkInterfaceProvider.myUniversalName = networkInterfaceProvider.availableInterfaces
					.firstElement().getHardwareAddress();
		return NetworkInterfaceProvider.networkInterfaceProvider;
	}

	public Vector<GenericNetworkInterface> getAvailableInterfaces() {
		return availableInterfaces;
	}

	public static String getMyUniversalName() {
		return myUniversalName;
	}
}
